$(function () {
   // 切换
   $("ul>li").click(function () {
      $(this).addClass('active').siblings().removeClass('active')
      console.log($(this).index());
      $(".bo").removeClass('active').eq($(this).index()).addClass('active')
   })

   // 注册

   //登录验证

   //手机号失焦验证

   let loginPhone = false

   $("#phone").on("blur", function () {
      let phone = $("#phone").val()
      if (!phone == "") {
         if ((/^1[34578]\d{9}$/.test(phone))) {
            $(this).next().css({ "display": "none" })
            $("#phone").css({ "border": "1px solid #dadada" })
            // $(location).attr('href', 'shou.html');
            loginPhone = true
         } else {
            $(this).next().css({ "color": "red", "display": "block", "padding-left": "76px" })
            $("#phone").css({ "border": "1px solid red" })
            loginPhone = false
         }
      } else {
         $(this).next().css({ "color": "red", "display": "block" })
         $("#phone").css({ "border": "1px solid red" })
         loginPhone = false

      }
   })


   $("#login").click(function () {
      if (loginPhone == true) {
         $.ajax({
            url: "http://localhost/login.php",
            type: "GET",
            data: {
               password: $("#loginmi").val(),
               userphonenumber: $("#phone").val()
            },
            success: function (res) {
               denglu(res)
            },
            dataType: "json",
         })
         function denglu(res) {
            var data = res
            if (data.type === "success") {
               open("shou.html");
            $(location).attr('href', 'shou.html');

            } else {
               if($("#phone").val() == ""){
                  $("#phone").next().css({ "color": "red", "display": "block" })
                  $("#phone").css({ "border": "1px solid red" })
               }
               $("#phone").css({ "border": "1px solid red" })
               $("#loginmi").next().css({ "color": "red", "display": "block" })
               $("#loginmi").css({ "border": "1px solid red" })
            }
         }
      }
   })


   //注册验证

   //定义全局记录验证
   let succesShouji = false
   let succesRegistermi = false
   let succesRegisterma = false
   let succesMaa = false
   let succesMa = false
   //手机号失焦验证
   $("#shouji").on("blur", function () {
      let phone = $("#shouji").val()
      if (!phone == "") {
         if ((/^1[34578]\d{9}$/.test(phone))) {
            $(this).next().css({ "display": "none" })
            $("#shouji").css({ "border": "1px solid #dadada" })
            succesShouji = true
         } else {
            $(this).next().css({ "color": "red", "display": "block" })
            $("#shouji").css({ "border": "1px solid red" })
            succesShouji = false
         }
      } else {
         $(this).next().css({ "color": "red", "display": "block" })
         $("#shouji").css({ "border": "1px solid red" })
         succesShouji = false
      }
   })

   //密码失焦验证
   $("#registermi").on("blur", function () {
      var regUpper = /[A-Z]/;
      var regLower = /[a-z]/;
      var regNum = /[0-9]/;
      var regTeShu = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？+-]");
      var complex = 0;
      const register = $("#registermi").val()
      if (!register == "") {
         if (regLower.test(register)) {
            ++complex;
         }
         if (regUpper.test(register)) {
            ++complex;
         }
         if (regNum.test(register)) {
            ++complex;
         }
         if (regTeShu.test(register)) {
            ++complex;
         }
         if (complex < 3 || register.length < 8) {
            $(this).next().css({ "color": "red", "display": "block" })
            $("#registermi").css({ "border": "1px solid red" })
            succesRegistermi = false
            return false
         } else {
            $(this).next().css({ "display": "none" })
            $("#registermi").css({ "border": "1px solid #dadada" })
            succesRegistermi = true

         }
      } else {
         $(this).next().css({ "color": "red", "display": "block" })
         $("#registermi").css({ "border": "1px solid red" })
         succesRegistermi = false
         return false
      }
   })



   //确认密码失焦验证
   $("#registerma").on("blur", function () {
      if (!$(this).val() == "") {
         $($(this).next()).css({ "display": "none" })
         $("#registerma").css({ "border": "1px solid #dadada" })
         if ($(this).val() !== $("#registermi").val()) {
            $(this).next().css({ "color": "red", "display": "block" })
            $(this).css({ "border": "1px solid red" })
            succesRegisterma = false
         } else {
            $(this).next().css({ "display": "none" })
            $("#registerma").css({ "border": "1px solid #dadada" })
            succesRegisterma = true

         }
      } else {
         $(this).next().css({ "color": "red", "display": "block" })
         $(this).css({ "border": "1px solid red" })
         succesRegisterma = false
      }
   })

   //验证码失焦验证
   $("#suiji").click(function () {
      $(this).text(random());
   })
   $("#maa").on("blur", function () {
      if (!$(this).val() == "") {
         if ($(this).val() !== $("#suiji").text()) {
            $(this).next().next().css({ "color": "red", "display": "block" })
            $(this).css({ "border": "1px solid red" })
            succesMaa = false
         } else {
            $(this).next().next().css({ "display": "none" })
            $(this).css({ "border": "1px solid #dadada" })
            succesMaa = true

         }
      } else {
         $(this).next().next().css({ "color": "red", "display": "block" })
         $(this).css({ "border": "1px solid red" })
         succesMaa = false
      }
   })

   //短信验证码失焦验证
   $("#sendMobileProSmsPopup").click(function () {
      $(this).text(random());
   })
   $("#ma").on("blur", function () {
      if (!$(this).val() == "") {
         if ($(this).val() !== $("#sendMobileProSmsPopup").text()) {
            $(this).next().next().css({ "color": "red", "display": "block" })
            $(this).css({ "border": "1px solid red" })
            succesMa = false
         } else {
            $(this).next().next().css({ "display": "none" })
            $(this).css({ "border": "1px solid #dadada" })
            succesMa = true
         }
      } else {
         $(this).next().next().css({ "color": "red", "display": "block" })
         $(this).css({ "border": "1px solid red" })
         succesMa = false
      }
   })

   //六位随机数
   function random() {
      var randomnumber = "";
      for (var t = 0; t < 6; t++) {
         randomnumber += parseInt(Math.random() * 10);
      }
      return randomnumber;
   }


   $("#register").click(function () {
      if (succesShouji == true && succesRegistermi == true && succesRegisterma == true && succesMaa == true && succesMa == true) {
         $.ajax({
            url: "http://localhost/register.php",
            type: "POST",
            data: {
               // useremail: $("#YXTRegisterForm_email").val(),
               password: $("#registermi").val(),
               userphonenumber: $("#shouji").val()
            },
            success: function (res) {
               zhuce(res)
            },
            dataType: "json",
         })
         function zhuce(res) {
            console.log(res);
            var data = res;
            if (data.type === "success") {
               open("den.html");
            } else {
               alert("请按要求进行注册")
            }
         }
      }
   })

})








